package com.wuyou.simple;

/**
 * 704. 二分查找
 */
public class Search {


    public static void main(String[] args) {
        int[] nums = {-1, 0, 3, 5, 9, 12};
        int target = 2;
//        int[] nums = {2, 5};
//        int target = 2;
        System.out.println(sol_1(nums, target));
    }

    public static int sol_1(int[] nums, int target) {
        int res = -1, l = 0, r = nums.length - 1, mid = nums.length / 2;
        while (l <= r) {
            if (target == nums[mid]) {
                res = mid;
                break;
            }
            if (nums[l] <= target && target < nums[mid]) {
                r = mid - 1;
                mid = ((r - l) / 2) + l;
            } else if (nums[mid] < target && target <= nums[r]) {
                l = mid + 1;
                mid = ((r - l) / 2) + l;
            }else {
                l++;
                r--;
            }
        }
        return res;
    }
}
